  
  /*************************************************************************
  * @file     main.c
  * @version  v1.0
  * @date     2024-01-19
  * @brief    main program
  *************************************************************************/


#include "at32f403a_407_clock.h"
#include "ap_task.h"
#include "stdio.h"


int fputc(int ch, FILE *f)
{
  while(usart_flag_get(USART3, USART_TDBE_FLAG) == RESET);
  usart_data_transmit(USART3, ch);
  return ch;
}

/**
  * @brief  initialize uart
  * @param  baudrate: uart baudrate
  * @retval none
  */
void uart_print_init(uint32_t baudrate)
{
	gpio_init_type gpio_init_struct;
	/* enable the uart and gpio clock */
	crm_periph_clock_enable(CRM_USART3_PERIPH_CLOCK, TRUE);
	crm_periph_clock_enable(CRM_GPIOB_PERIPH_CLOCK, TRUE);

	gpio_default_para_init(&gpio_init_struct);

	/* configure the usart3 tx pin */
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_mode = GPIO_MODE_MUX;
	gpio_init_struct.gpio_pins = GPIO_PINS_10;
	gpio_init_struct.gpio_pull = GPIO_PULL_NONE;
	gpio_init(GPIOB, &gpio_init_struct);
	/* configure the usart3 rx pin */
	gpio_init_struct.gpio_drive_strength = GPIO_DRIVE_STRENGTH_STRONGER;
	gpio_init_struct.gpio_out_type  = GPIO_OUTPUT_PUSH_PULL;
	gpio_init_struct.gpio_mode = GPIO_MODE_INPUT;
	gpio_init_struct.gpio_pins = GPIO_PINS_11;
	gpio_init_struct.gpio_pull = GPIO_PULL_UP;
	gpio_init(GPIOB, &gpio_init_struct);

	/* configure uart param */
	usart_init(USART3, baudrate, USART_DATA_8BITS, USART_STOP_1_BIT);
	usart_transmitter_enable(USART3, TRUE);
	usart_enable(USART3, TRUE);
}

/**
  * @brief  main function.
  * @param  none
  * @retval none
  */
int main(void)
{
	nvic_priority_group_config(NVIC_PRIORITY_GROUP_4);
	__enable_irq();
	/* init the clock for 240MHz */
	system_clock_config();
	uart_print_init(115200);
	hardware_init();
	creat_task();
	
	printf("run out rtos\r\n");
	while(1)
	{
		
	}
}

void hard_fault_handler_c(unsigned int * hardfault_args,unsigned lr_value) 
{
    printf("Enter HardFault_Handler\r\n");
    unsigned int stacked_r0;
    unsigned int stacked_r1;
    unsigned int stacked_r2;
    unsigned int stacked_r3;
    unsigned int stacked_r12;
    unsigned int stacked_lr;
    unsigned int stacked_pc;
    unsigned int stacked_psr;
    stacked_r0 = ((unsigned long)hardfault_args[0]);
    stacked_r1 = ((unsigned long)hardfault_args[1]);
    stacked_r2 = ((unsigned long)hardfault_args[2]);
    stacked_r3 = ((unsigned long)hardfault_args[3]);
    stacked_r12 = ((unsigned long)hardfault_args[4]);
    stacked_lr = ((unsigned long)hardfault_args[5]);
    stacked_pc = ((unsigned long)hardfault_args[6]);
    stacked_psr = ((unsigned long)hardfault_args[7]);
    
    printf("R0 = %08x\r\n",stacked_r0);
    printf("R1 = %08x\r\n",stacked_r1);
    printf("R2 = %08x\r\n",stacked_r2);
    printf("R3 = %08x\r\n",stacked_r3);
    printf("R12 = %08x\r\n",stacked_r12);
    printf("Stacked LR = %08x\r\n",stacked_lr);
    printf("Stacked PC = %08x\r\n",stacked_pc);
    printf("Stacked PSR = %08x\r\n",stacked_psr);
    printf("Current LR = %08x\r\n",lr_value);
    while(1)
	{
	
	}
}

